package com.example.knowledgesupplement;

/**
 * 取模，位运算，+：速度：+>位运算>取模
 */
public class BitAndModulusTest {

    public static void main(String[] args) {
        int number=1;
        for (int i = 0; i <3 ; i++) {
            System.out.println("第----------"+(i+1)+"次比较：--------------");
            number*=1000;
            System.out.println("number："+number);
            BitAndModulusTest.bit(number);
            BitAndModulusTest.modulus(number);
            BitAndModulusTest.add(number);

        }

    }

    private static void add(int number) {
        int a = 1;
        long start = System.currentTimeMillis();
        for (int i = number; i > 0; i++) {
            a += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("加法运算耗时： " + (end - start));
    }

    /**
     * 位运输
     * @param number
     */
    public static void bit(int number) {
        int a = 1;
        long start = System.currentTimeMillis();
        for (int i = number; i > 0; i++) {
            a &= i;
        }
        long end = System.currentTimeMillis();
        System.out.println("位运算耗时： " + (end - start));
    }

    /**
     * 取模运输
     * @param number
     */
    public static void modulus(int number) {
        int a = 1;
        long start = System.currentTimeMillis();
        for (int i = number; i > 0; i++) {
            a %= i;
        }
        long end = System.currentTimeMillis();
        System.out.println("取模运算耗时： " + (end - start));
    }
}
